<template>
    <div class="user">
        <el-button type="primary" @click="handleAdd">+ 新增</el-button>
        <el-dialog title="新增用户" :visible.sync="dialogVisible" width="50%" :before-close="handleClose">
            <!-- 表单 -->
            <el-form ref="form" :model="form" label-width="80px" :inline="true" :rules="rules">
                <el-form-item label="姓名" prop="name">
                    <el-input v-model="form.name" placeholder="请输入姓名"></el-input>
                </el-form-item>
                <el-form-item label="年龄" prop="age">
                    <el-input v-model="form.age" placeholder="请输入年龄"></el-input>
                </el-form-item>
                <el-form-item label="性别" prop="sex">
                    <el-select v-model="form.sex" placeholder="请选择性别">
                        <el-option label="男" :value="1"></el-option>
                        <el-option label="女" :value="0"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="出生日期" prop="birth">
                    <el-date-picker type="date" placeholder="选择日期" v-model="form.birth" style="width: 100%;"
                        value-format="yyyy-MM-DD"></el-date-picker>
                </el-form-item>
                <el-form-item label="地址" prop="addr">
                    <el-input v-model="form.addr" placeholder="请输入地址"></el-input>
                </el-form-item>

            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="dialogVisible = false">取 消</el-button>
                <el-button type="primary" @click="submit">确 定</el-button>
            </span>
        </el-dialog>

        <!-- 表格 -->
        <el-table :data="tableData" style="width: 100%" border height="100%">
            <el-table-column prop="name" label="姓名">
            </el-table-column>
            <el-table-column prop="sex" label="性别">
                <template slot-scope="scope">
                    <span style="margin-left: 10px">{{ scope.row.sex === 1 ? "男" : "女" }}</span>
                </template>
            </el-table-column>
            <el-table-column prop="age" label="年龄">
            </el-table-column>
            <el-table-column prop="birth" label="出生日期">
            </el-table-column>
            <el-table-column prop="addr" label="地址">
            </el-table-column>
            <el-table-column>
                <template slot-scope="scope">
                    <el-button size="mini" @click="handleEdit(scope.row)">编辑</el-Button>
                    <el-button size="mini" @click="handleDelete(scope.row)" type="danger">删除</el-Button>
                </template>
            </el-table-column>
        </el-table>

        <div class="pagination">
            <el-pagination layout="prev, pager, next" :total="total" @current-change="handlePage">
            </el-pagination>
        </div>
    </div>
</template>

<script>
import { getUser, addUser, editUser, delUser } from "@/api/index";
import { Button } from "element-ui";
export default {
    data() {
        return {
            dialogVisible: false,
            form: {
                name: "",
                age: "",
                sex: "",
                birth: "",
                addr: ""
            },
            rules: {
                name: [
                    { required: true, message: '请输入姓名' }
                ],
                age: [
                    { required: true, message: '请输入年龄' }
                ],
                sex: [
                    { required: true, message: '请选择性别' }
                ],
                birth: [
                    { required: true, message: '请选择出生日期' }
                ],
                addr: [
                    { required: true, message: '请输入地址' }
                ],
            },
            tableData: [],
            modeType: 0,//0--新增  1--编辑
            total: 0,//总条数
            pageData: {
                page: 1,
                limit: 10
            }
        };
    },
    methods: {
        handleClose(done) {
            this.$confirm('确认关闭？')
                .then(_ => {
                    done();
                })
                .catch(_ => { });
            // 清空表单数据
            this.$refs.form.resetFields();
        },
        submit() {
            this.$refs.form.validate(valid => {
                // console.log("valid", valid);

                if (this.modeType === 0) {//新增
                    addUser(this.form).then(() => {
                        // 重新获取列表的接口
                        this.getList()
                    })

                } else {//编辑
                    editUser(this.form).then(() => {
                        // 重新获列表的接口
                        this.getList()
                    })

                }

                if (valid) {
                    // 清空表单数据
                    this.$refs.form.resetFields();
                    // 关闭弹窗
                    this.dialogVisible = false;
                }
            });
        },
        // 新增
        handleAdd() {
            this.modeType = 0;
            this.dialogVisible = true;
        },
        // 编辑
        handleEdit(row) {
            this.modeType = 1;
            this.dialogVisible = true;
            // 注意需要对当前行数据进行深拷贝
            this.form = JSON.parse(JSON.stringify(row))

        },
        // 删除
        handleDelete(row) {
            this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                delUser({ id: row.id }).then(() => {
                    this.$message({
                        type: 'success',
                        message: '删除成功!'
                    });
                    this.getList()
                })

            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消删除'
                });
            });

        },
        // 封装获取列表的数据
        getList() {
            getUser(this.pageData).then(res => {
                // console.log('列表数据---', res.data.list);
                this.tableData = res.data.list;

                this.total = res.data.count || 0
            });
        },
        // 选择页码的回调函数
        handlePage(val) {
            console.log("页码", val);
            this.pageData.page = val;
            this.getList()

        }
    },
    mounted() {
        this.getList()

    },
    components: { Button }
};
</script>

<style lang="less" scoped>
.user {
    height: 90%;
}
.pagination{
    text-align: right;
}
</style>